package edu.sctu.zhoumengxue.day201506.day20150616.urlPractice;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created by user12 on 2015/6/16.
 */
public class UrlConnectionTest {

    public void urltest(String path){
        int count = 0;
        byte[] bytes = new byte[1024];
        try {
            URL url = new URL(path);
            try {
                InputStream in =url.openStream();
                while ((count=in.read(bytes))!=-1){
                    System.out.println(new String(bytes,0,count,"utf-8"));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
    public void urltest2(String path){
        int count = 0;
        byte[] bytes = new byte[1024];
        try {
            URL url = new URL(path);
            try {
                URLConnection connection = url.openConnection();
                connection.connect();
                InputStream in = connection.getInputStream();
                while ((count=in.read(bytes))!=-1){
                    System.out.println(new String(bytes,0,count,"utf-8"));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

}
